package com.tcc.naozhongservice;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.widget.Toast;

import com.tcc.mynote.R;

import java.io.IOException;

public class AlarmAlert extends Activity {

    private static final float IN_CALL_VOLUME = 0.125f;
    private Intent intent;
    private MediaPlayer mp;
    private Vibrator mVibrator;
    private AudioManager audioManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        intent = getIntent();
        String[] cInfo = intent.getStringArrayExtra("cInfo");

        mp = new MediaPlayer();

        mVibrator = (Vibrator) getApplication().getSystemService(VIBRATOR_SERVICE);
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

        System.out.println(cInfo.length);
        if (cInfo.length > 4) {
            String week = cInfo[0];
            String whichLesson = cInfo[1];
            String cName = cInfo[2];
            if (cName.equals("有课")) {
                cName = "";
            }
            String address = cInfo[3];
            String startTime = cInfo[4];
            String endTime = cInfo[5];
            String aheadTime = cInfo[6];
            String isRemind = cInfo[7];
            final String isRemindByVibrato = cInfo[8];
            final String isRemindByRing = cInfo[9];
            final String teacher = cInfo[10];

//		    System.out.println("............." + isRemindByVibrato + "," + isRemindByRing);

            openRemind(isRemindByVibrato, isRemindByRing);

            new AlertDialog.Builder(AlarmAlert.this)
                    .setTitle("还有 " + aheadTime + " 分钟哦，准备好了吗？")
                    .setMessage("星期" + week + "  " + whichLesson + "\n" +
                            "课程名称：" + cName + "\n" +
                            "上课地点：" + address + "\n" +
                            "课堂时间：" + startTime + "-" + endTime + "\n" +
                            "任课老师：" + teacher)
                    .setPositiveButton("关掉提醒",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    closeRemind(isRemindByVibrato, isRemindByRing);
                                    AlarmAlert.this.finish();
                                }
                            })
                    .setNegativeButton("我知道了",
                            new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int which) {
                                    // TODO Auto-generated method stub
                                    closeRemind(isRemindByVibrato, isRemindByRing);
                                    AlarmAlert.this.finish();
                                }
                            })
                    .show();
        } else {
            String content = cInfo[0];
            String remindTime = cInfo[1];
            final String isRemindByVibrato = cInfo[2];
            final String isRemindByRing = cInfo[3];

            openRemind(isRemindByVibrato, isRemindByRing);

            new AlertDialog.Builder(AlarmAlert.this)
                    .setTitle("计划提醒")
                    .setMessage(content + "\n" + remindTime)
                    .setPositiveButton("关掉提醒", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            closeRemind(isRemindByVibrato, isRemindByRing);
                            AlarmAlert.this.finish();
                        }
                    }).show();
        }
    }

    // 开始提醒
    public void openRemind(String isRemindByVibrato, String isRemindByRing) {
        if (isRemindByVibrato.equals("true") && isRemindByRing.equals("true")) {
            startVibratoAlarm(mVibrator);
            try {
                mp.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);
                setDataSourceFromResource(getResources(), mp, R.raw.in_call_alarm);
                startAudioAlarm(mp);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.out.println("startAudioAlarm startVibratoAlarm");
            Toast.makeText(AlarmAlert.this, "震动和铃声", Toast.LENGTH_LONG);

        } else if (isRemindByVibrato.equals("true") && isRemindByRing.equals("false")) {

            startVibratoAlarm(mVibrator);
            System.out.println("startVibratoAlarm");
            Toast.makeText(AlarmAlert.this, "震动", Toast.LENGTH_LONG);

        } else if (isRemindByVibrato.equals("false") && isRemindByRing.equals("true")) {
            try {
                mp.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);
                setDataSourceFromResource(getResources(), mp, R.raw.in_call_alarm);
                startAudioAlarm(mp);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.out.println("startAudioAlarm");
            Toast.makeText(AlarmAlert.this, "铃声", Toast.LENGTH_LONG);

        }
    }

    // 关闭提醒
    public void closeRemind(String isRemindByVibrato, String isRemindByRing) {
        if (isRemindByVibrato.equals("true") && isRemindByRing.equals("true")) {
            stopVibratoAlarm(mVibrator);
            stopAudioAlarm(mp);
            System.out.println("stopVibratoAlarm stopAudioAlarm");
            Toast.makeText(AlarmAlert.this, "关闭震动和铃声", Toast.LENGTH_LONG);
        } else if (isRemindByVibrato.equals("true") && isRemindByRing.equals("false")) {
            stopVibratoAlarm(mVibrator);
            Toast.makeText(AlarmAlert.this, "关闭震动", Toast.LENGTH_LONG);
            System.out.println("stopVibratoAlarm");
        } else if (isRemindByVibrato.equals("false") && isRemindByRing.equals("true")) {
            stopAudioAlarm(mp);
            Toast.makeText(AlarmAlert.this, "关闭铃声", Toast.LENGTH_LONG);
            System.out.println("stopAudioAlarm");
        }
    }

    // 播放铃声
    public void startAudioAlarm(MediaPlayer player) throws java.io.IOException, IllegalArgumentException,
            IllegalStateException {
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            player.setAudioStreamType(AudioManager.STREAM_ALARM);
            player.setLooping(true);
            player.prepare();
            player.start();
        }
    }

    // 关闭铃声
    public void stopAudioAlarm(MediaPlayer mp) {

        if (mp != null) {
            mp.stop();
            mp.release();
        }

    }

    // 开始震动
    public void startVibratoAlarm(Vibrator mVibrator) {
        mVibrator.vibrate(new long[]{500, 5000, 500, 5000, 500, 5000}, 0);
    }

    // 关闭震动
    public void stopVibratoAlarm(Vibrator mVibrator) {
        mVibrator.cancel();
    }

    // 设置铃声的资源
    private void setDataSourceFromResource(Resources resources,
                                           MediaPlayer player, int res) throws java.io.IOException {
        AssetFileDescriptor afd = resources.openRawResourceFd(res);
        if (afd != null) {
            player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
                    afd.getLength());
            afd.close();
        }
    }
}
